package com.weavernorth.action.jb;
import com.weavernorth.action.jb.utils.LastDayOfMonth;
import com.weavernorth.action.jb.utils.sendDataToSapUtils;
import com.weavernorth.util.Logger4jUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


/**
 * @Author: bruseewang
 * @Package: com.weavernorth.action.jb
 * @date: 2020/5/14 11:27
 * @description: 加班工资申请流程--加班时数推送至SAP
 */
public class jbHoursToSAP extends BaseBean implements Action {
    //定义日志对象
//    private static Log log = LogFactory.getLog(jbHoursToSAP.class);
    private Logger log= Logger4jUtil.getLoggerByName("/action/api/jbHoursToSAP/");

    /**
     * 流程路径节点后选择aciton后,会在节点提交后执行此方法。
     */
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs=new RecordSet();
        RecordSet rs_detail=new RecordSet();
        RecordSet rs_fgs_jbInfo=new RecordSet();
        BaseBean bsb=new BaseBean();
        bsb.writeLog("加班传数接口执行开始----：");
        //请求id
        String strRequestid = requestInfo.getRequestid();
        bsb.writeLog("加班流程requestid:"+strRequestid);
        //表名
        bsb.writeLog("获取表名----：");
        String strTable = requestInfo.getRequestManager().getBillTableName();
        //SAP接口地址；获取属性文件的值，s:属性文件名称,s1:文件中的属性
        bsb.writeLog("获取接口地址----：");
        String sapUrl = bsb.getPropValue("jbtosap", "jbtosapurl");
        //主表id
        String strMainid=null;
        String strSqr=null;
        String strSqrq=null;
        int strNf=0;
        int strMy=0;
        String strDwj=null;
        String strJbjs=null;
        String strFgshppdy=null;
        String strDate=null;


        //根据requestid获取主表信息
        String strMain = " select * from " + strTable + " where requestid=?";
        bsb.writeLog("获取主表信息----：");
        rs.executeQuery(strMain,strRequestid);
        while (rs.next()){
            strMainid=rs.getString("id");
            strSqr=rs.getString("sqr");//申请人
            strSqrq=rs.getString("sqrq");//申请日期
            //DateUtil
            strNf= Util.getIntValue(rs.getString("nf"),0) ;//年份
            strMy= Util.getIntValue(rs.getString("yf"),0);//月份
            strDwj=rs.getString("dwj");//淡旺季
            strJbjs=rs.getString("jbjs");//加班基数
            strFgshppdy=rs.getString("fgshppdy");//分公司品牌单元
            //取表单中年份下某月的最后一天
            bsb.writeLog("获取某月最后一天----：");
            try {
                bsb.writeLog("年："+strNf+",月："+strMy+"----：");
                strDate= LastDayOfMonth.LastDayOfMonth(strNf,strMy);
            }catch (Exception e){
                bsb.writeLog("获取信息异常----："+e.getMessage()+"");
                return FAILURE_AND_CONTINUE;
            }

            bsb.writeLog("最后一天是----："+strDate+"");
        }

        //判断该分公司或品牌单元是否在码表中维护
        String strFgsMb_ffd=null;//分公司码表中非法定节假日编码
        String strFgsMb_fd=null;//分公司码表中法定节假日编码
        Boolean strIsExists=false;//判断分公司是否在码表维护加班类型编码
        String strFgs_Sql="SELECT * from uf_jbtssapmb WHERE fgshppdy=?";
        bsb.writeLog("获取分公司码表信息----：");
        rs_fgs_jbInfo.executeQuery(strFgs_Sql,strFgshppdy);
        while (rs_fgs_jbInfo.next()){
            strIsExists=true;
            strFgsMb_ffd=rs_fgs_jbInfo.getString("ffdjjr");
            strFgsMb_fd=rs_fgs_jbInfo.getString("fdjjr");
        }
        bsb.writeLog("获取的分公司码表信息为：ffdjjr(非法定节假日)："+strFgsMb_ffd+",fdjjr(法定节假日)："+strFgsMb_fd+"");
        //获取子表信息
        if (strMainid!=null){
            String strDetail="select * from "+strTable+"_dt1 where mainid=?";
            rs_detail.executeQuery(strDetail,strMainid);

            //推送SAP 的xml拼接
            StringBuffer strRequestXml = new StringBuffer();
            strRequestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:sap-com:document:sap:rfc:functions'>");
            strRequestXml.append("<soapenv:Header/>");
            strRequestXml.append("<soapenv:Body>");
            strRequestXml.append("<urn:ZHRI_084>");
            strRequestXml.append("<T_P2010>");
            while (rs_detail.next()){
                //员工编号
                String strYgbh=rs_detail.getString("ygbh");
                //员工姓名
                String strYgxm=rs_detail.getString("ygxm");
                //非法定加班总时数
                String strFfjbzss=rs_detail.getString("ffjbzss");
                //转调休时数
                String strZtxss=rs_detail.getString("zdxss");
                //法定加班总时数
                //系统法定加班数xtfdjbss
                String strFjbzss=rs_detail.getString("xtfdjbss");
                Double isEmpty_fd= Util.getDoubleValue(strFjbzss,0.00);
                //转加班总时数
                String strzjbgzss=rs_detail.getString("zjbgzss");
                Double isEmpty_ffd= Util.getDoubleValue(strzjbgzss,0.00);
                //将数据传送至SAP
                bsb.writeLog("是否将数据传送至SAP:"+strIsExists);
                if (strIsExists){
                    //非法定节假日加班传至SAP
                    bsb.writeLog("非法定节假日加班是否传至SAP:strFgsMb_ffd="+strFgsMb_ffd);
                    if (strFgsMb_ffd!=null||"".equals(strFgsMb_ffd)) {
                        bsb.writeLog("转加班(非法定)总时数的值:isEmpty_ffd="+isEmpty_ffd);
                        if (isEmpty_ffd!=0.00) {
                            // TODO: 2020/5/14
                            bsb.writeLog("非法定加班日志记录----：");
                            strRequestXml.append("<item>");
                            strRequestXml.append("<PERNR>" + strYgbh + "</PERNR>");//员工编号
                            strRequestXml.append("<BEGDA>" + strDate + "</BEGDA>");//开始日期
                            strRequestXml.append("<LGART>" + strFgsMb_ffd + "</LGART>");//工资项
                            strRequestXml.append("<ANZHL>" + strzjbgzss + "</ANZHL>");//数值
                            strRequestXml.append("</item>");
                            //String requestXml=sendDataToSapUtils.buildRequestXml(strYgbh,strDate,strFgsMb_ffd,strFfjbzss);
                            //bsb.writeLog("请求体xml----："+requestXml+"");
                        }
                        else{
                            bsb.writeLog("员工"+strYgbh+"的转加班总时数为0,不传至SAP");
                        }
                    }
                    bsb.writeLog("法定节假日加班是否传至SAP:strFgsMb_ffd="+strFgsMb_fd);
                    //法定节假日加班传送至SAP
                    if (strFgsMb_fd!=null||"".equals(strFgsMb_fd)) {
                        bsb.writeLog("法定节假日加班时数的值:isEmpty_fd="+isEmpty_fd);
                        if (isEmpty_fd!=0.00) {
                            bsb.writeLog("非法定加班日志记录----：");
                            // TODO: 2020/5/14
                            //String requestXml=sendDataToSapUtils.buildRequestXml(strYgbh,strDate,strFgsMb_fd,strFfjbzss);
                            //String[] strResult=null;
                            strRequestXml.append("<item>");
                            strRequestXml.append("<PERNR>" + strYgbh + "</PERNR>");//员工编号
                            strRequestXml.append("<BEGDA>" + strDate + "</BEGDA>");//开始日期
                            strRequestXml.append("<LGART>" + strFgsMb_fd + "</LGART>");//工资项
                            strRequestXml.append("<ANZHL>" + strFjbzss + "</ANZHL>");//数值
                            strRequestXml.append("</item>");
                        }
                        else{
                            bsb.writeLog("员工"+strYgbh+"法定节假日加班时数为0,不传至SAP");
                        }
                    }
                }else{
                    log.info("该分公司/单元没有在码表维护加班类型编码");
                }
            }
            strRequestXml.append("</T_P2010>");
            strRequestXml.append("<T_RETURN>");
            strRequestXml.append("<item>");
            strRequestXml.append("<TYPE></TYPE>");
            strRequestXml.append("<ID></ID>");
            strRequestXml.append("<NUMBER></NUMBER>");
            strRequestXml.append("<MESSAGE></MESSAGE>");
            strRequestXml.append("<LOG_NO></LOG_NO>");
            strRequestXml.append("<LOG_MSG_NO></LOG_MSG_NO>");
            strRequestXml.append("<MESSAGE_V1></MESSAGE_V1>");
            strRequestXml.append("<MESSAGE_V2></MESSAGE_V2>");
            strRequestXml.append("<MESSAGE_V3></MESSAGE_V3>");
            strRequestXml.append("<MESSAGE_V4></MESSAGE_V4>");
            strRequestXml.append("</item>");
            strRequestXml.append("</T_RETURN>");
            strRequestXml.append("</urn:ZHRI_084>");
            strRequestXml.append("</soapenv:Body>");
            strRequestXml.append("</soapenv:Envelope>");
            bsb.writeLog("节假日加班SAP请求体："+strRequestXml.toString());
            String[] strResult=null;
            try {
                //判断请求体是否有要传的参数,如果没有则直接继续返回成功，不去调用传送到sAP
                if(sendDataToSapUtils.eleisExists(strRequestXml.toString(),"//PERNR"))
                {
                    bsb.writeLog("开始调用sendPost方法");
                    String responseXml= sendDataToSapUtils.sendPost(sapUrl,strRequestXml.toString(),"utf-8","jbtosap");
                    if(responseXml==null){
                        requestInfo.getRequestManager().setMessagecontent("请求超时");
                        return FAILURE_AND_CONTINUE;
                    }
                    bsb.writeLog("节假日加班SAP返回信息："+responseXml);

                    strResult=sendDataToSapUtils.responseResult(responseXml.toString());
                    String strCode=strResult[0];
                    String strMsg=strResult[1];
                    bsb.writeLog("接口返回消息状态码："+strCode+";接口返回消息:"+strMsg);
                    if (strCode.equals("s")){
                        // TODO: 2020/5/18  继续
                        return SUCCESS;
                    }else if (strCode.equals("f")){
                        if (!"".equals(strMsg)) {
                            requestInfo.getRequestManager().setMessagecontent(strMsg);
                        }
                        return FAILURE_AND_CONTINUE;
                    }
                }
                else{
                    bsb.writeLog("没有要传送至SAP的信息,直接返回成功！");
                    return SUCCESS;
                }

            }catch (Exception e){
                log.info("节假日加班数据传送至SAP异常："+e.toString());
                e.printStackTrace();
                requestInfo.getRequestManager().setMessagecontent("节假日加班数据传送至SAP异常"+e.toString());
                return FAILURE_AND_CONTINUE;
            }
        }
        else{
            log.info("未找到主表信息");
            requestInfo.getRequestManager().setMessagecontent("未找到主表信息");
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
